package com.sxrong.carloan.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sxrong.carloan.R;
import com.sxrong.carloan.activity.CarInfoActivity;
import com.sxrong.carloan.adapter.CarAdapter;
import com.sxrong.carloan.dto.CarInfoBean;
import com.sxrong.carloan.dto.CarInfoModel;
import com.sxrong.carloan.net.ApiService;
import com.sxrong.carloan.net.MyCallBack;
import com.sxrong.carloan.net.XUtils;
import com.sxrong.carloan.utils.AppCacheSharedPreferences;
import com.sxrong.carloan.utils.CommonListener;
import com.sxrong.carloan.utils.Config;
import com.sxrong.carloan.utils.EndlessRecyclerOnScrollListener;

import java.util.HashMap;

/**
 * Created by LakNeumann on 2018/3/23.
 */

public class CarFragment extends Fragment {
    private int page = 0 ;

    private ClickListener listener;
    private CarAdapter adapter;
    private RecyclerView recycleView;
    private SwipeRefreshLayout refresh;
    private boolean is_loading = false;

    public static CarFragment newInstance(int type) {

        Bundle args = new Bundle();
        args.putInt("type" , type);
        CarFragment fragment = new CarFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if(context instanceof ClickListener){
            listener = (ClickListener)context;
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_car , null);
        recycleView = view.findViewById(R.id.recycleView);
        refresh = view.findViewById(R.id.refresh);
        recycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new CarAdapter(getActivity());
        recycleView.setAdapter(adapter);
        adapter.setCommonListener(new CommonListener<CarInfoBean>() {
            @Override
            public void onItemClick(int pos, CarInfoBean data) {

                listener.clickItem(data);

            }
        });
        recycleView.addOnScrollListener(new EndlessRecyclerOnScrollListener((LinearLayoutManager) recycleView.getLayoutManager()) {
            @Override
            public void onLoadMore() {
                Toast.makeText(getContext(), "正在加载中", Toast.LENGTH_SHORT).show();
                if(is_loading){
                    return;
                }
                is_loading = true;
                page += 1;
                getCardInfo();
            }
        });
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 0;
                getCardInfo();
            }
        });
        getCardInfo();
        return view;
    }


    public void getCardInfo() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("partner", AppCacheSharedPreferences.getCacheString(Config.PARTNER_ID));
        hashMap.put("state" , getArguments().getInt("type") + "");
        hashMap.put("page" , page + "");
        hashMap.put("size" , 10 + "");
        XUtils.Get(ApiService.CAR_URL, hashMap, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                refresh.setRefreshing(false);
                CarInfoModel model = new Gson().fromJson(result, CarInfoModel.class);
                if(adapter == null){
                    return;
                }
                if(page == 0) {
                    adapter.setList(model.getData());
                }else{
                    is_loading = false;
                    adapter.addList(model.getData());
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                if(refresh != null) {
                    refresh.setRefreshing(false);
                }
                if(page != 0){
                    is_loading = false;
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }

    public interface ClickListener{
        void clickItem(CarInfoBean bean);

        void addItem();
    }
}
